package com.e3.basdoc.cust.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.e3.basdoc.bank.client.BankRefDialog;
import com.e3.basdoc.bank.model.BankModel;
import com.e3.basdoc.currency.client.CurrencyRefDialog;
import com.e3.basdoc.cust.model.CustAccountFields;
import com.e3.basdoc.cust.model.CustAccountModel;
import com.e3.basdoc.cust.model.CustConstant;
import com.e3.basdoc.cust.model.CustFields;
import com.e3.basdoc.cust.model.CustModel;
import com.e3.basdoc.region.client.RegionRefDialog;
import com.e3.template.tgf.TGFHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.e3.web.widget.client.MyListCellEditor;
import com.e3.web.widget.client.MyListField;
import com.e3.web.widget.client.MyRefCellEditor;
import com.e3.web.widget.client.MyRefTextField;
import com.e3.web.widget.model.ListTypeModel;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

public class CustHelper extends TGFHelper<CustModel>{
	
	public static CustHelper INSTANCE = new CustHelper();
	
	private String[] notNullFields = {CustModel.CODE,CustModel.NAME,CustModel.SHORT_NAME};
	
	private Map<String,Integer> lengthMap = new HashMap<String, Integer>();
	
	private Map<String,Integer> accLengthMap = new HashMap<String, Integer>();
	
	private CustHelper(){
		lengthMap.put(CustModel.ID, 19);
		lengthMap.put(CustModel.CODE, 20);
		lengthMap.put(CustModel.NAME, 50);
		lengthMap.put(CustModel.SHORT_NAME, 20);
		lengthMap.put(CustModel.TYPE, 2);
		lengthMap.put(CustModel.PARENT_CUST, 19);
		lengthMap.put(CustModel.CONTACT_ADDRESS, 100);
		lengthMap.put(CustModel.EMAIL, 50);
		lengthMap.put(CustModel.CONTACT_PERSON, 50);
		lengthMap.put(CustModel.CONTACT_WAY, 100);
		lengthMap.put(CustModel.FAX, 20);
		lengthMap.put(CustModel.BUSINESS_ADDRESS, 100);
		lengthMap.put(CustModel.REGISTER_ADDRESS, 100);
		lengthMap.put(CustModel.TELPHONE, 20);
		lengthMap.put(CustModel.ZIPCODE, 20);
		lengthMap.put(CustModel.REGISTER_NO, 30);
		lengthMap.put(CustModel.TAX_NO, 30);
		lengthMap.put(CustModel.LEGAL_PERSON, 20);
		lengthMap.put(CustModel.LEGAL_PERSON_IDCARD, 20);
		lengthMap.put(CustModel.REMARK, 100);
		
		accLengthMap.put(CustAccountModel.ACCOUNTNO, 50);
		accLengthMap.put(CustAccountModel.REMARK, 100);
	}


	public Field<?> createFormField(String name) {
		Field<?> formField = null;
		String dispName = getCustFieldDisplayName(name);
		boolean isNotNull = isNotNullField(name);
		
		if (name.equals(CustFields.CODE) || name.equals(CustFields.NAME) || name.equals(CustFields.SHORT_NAME)
				|| name.equals(CustFields.BUSINESS_ADDRESS) || name.equals(CustFields.CONTACT_ADDRESS) || name.equals(CustFields.TELPHONE)
				|| name.equals(CustFields.EMAIL) || name.equals(CustFields.ZIPCODE) || name.equals(CustFields.FAX)
				|| name.equals(CustFields.REGISTER_ADDRESS) || name.equals(CustFields.REGISTER_NO) || name.equals(CustFields.TAX_NO)
				|| name.equals(CustFields.CONTACT_PERSON) || name.equals(CustFields.CONTACT_WAY) || name.equals(CustFields.LEGAL_PERSON)
				|| name.equals(CustFields.LEGAL_PERSON_IDCARD)|| name.equals(CustAccountFields.REMARK)) {
			int length = lengthMap.get(name);
			formField = MyFormFieldFactory.createStringField(name, dispName,length,isNotNull);
		}
		else if (name.equals(CustFields.CORP_RELATIONSHIP)) {
			List<ListTypeModel<Integer>> listValueModels = new ArrayList<ListTypeModel<Integer>>();
			// 公司关系：(1-客户，2-供应商，3-客商)
			listValueModels.add(new ListTypeModel<Integer>(CustConstant.CUST_FLAG_CUSTOMER, CustFieldsConstant.INSTANCE.corp_relationship_customer()));
			listValueModels.add(new ListTypeModel<Integer>(CustConstant.CUST_FLAG_SUPPLIER, CustFieldsConstant.INSTANCE.corp_relationship_supplier()));
			listValueModels.add(new ListTypeModel<Integer>(CustConstant.CUST_FLAG_BOTH, CustFieldsConstant.INSTANCE.corp_relationship_businessParty()));
			formField = MyFormFieldFactory.createListField(name, dispName, listValueModels, true,isNotNull);
		}
		else if (name.equals(CustFields.PARENT_CUST)) {
			formField = MyFormFieldFactory.createRefField(name, dispName, new CustRefDialog(),isNotNull);
		}
		else if (name.equals(CustFields.REGION)) {
			formField = MyFormFieldFactory.createRefField(name, dispName, new RegionRefDialog(),isNotNull);
		}
		else if (name.equals(CustFields.CURRENCY)) {
			formField = MyFormFieldFactory.createRefField(name, dispName, new CurrencyRefDialog(),isNotNull);
		}
		else if (name.equals(CustFields.TYPE)) {
			List<ListTypeModel<Integer>> listValueModels = new ArrayList<ListTypeModel<Integer>>();
			// 企业法人（1）、分支机构（2）、个体（3）、其他（4）
			listValueModels.add(new ListTypeModel<Integer>(CustConstant.CUST_TYPE_LEGALPERSION, CustFieldsConstant.INSTANCE.type_legal_person()));
			listValueModels.add(new ListTypeModel<Integer>(CustConstant.CUST_TYPE_BRANCH, CustFieldsConstant.INSTANCE.type_branch()));
			listValueModels.add(new ListTypeModel<Integer>(CustConstant.CUST_TYPE_INDIVIDUAL, CustFieldsConstant.INSTANCE.type_individual()));
			listValueModels.add(new ListTypeModel<Integer>(CustConstant.CUST_TYPE_OTHERS, CustFieldsConstant.INSTANCE.type_others()));
			formField = MyFormFieldFactory.createListField(name, dispName, listValueModels, true,isNotNull);
		}
		else if (name.equals(CustFields.TAXPAYER_TYPE)) {
			List<ListTypeModel<Integer>> listValueModels = new ArrayList<ListTypeModel<Integer>>();
			listValueModels.add(new ListTypeModel<Integer>(CustConstant.CUST_TAX_COMMON, CustFieldsConstant.INSTANCE.taxpayer_type_common()));
			listValueModels.add(new ListTypeModel<Integer>(CustConstant.CUST_TAX_SMALL, CustFieldsConstant.INSTANCE.taxpayer_type_small_scale()));
			formField = MyFormFieldFactory.createListField(name, dispName, listValueModels, true,isNotNull);
		}
		else if (name.equals(CustFields.FOUND_TIME) || name.equals(CustFields.REGISTER_START_TIME)
				|| name.equals(CustFields.REGISTER_END_TIME)) {
			formField = MyFormFieldFactory.createDateField(name, dispName,isNotNull);
		}
		else if (name.equals(CustFields.REMARK)) {
			int length = lengthMap.get(name);
			formField = MyFormFieldFactory.createTextAreaField(name, dispName,length,isNotNull);
		}
		else if (name.equals(CustFields.ID)) {
			formField = MyFormFieldFactory.createIdField(name);
		}
		return formField;
	}

	public Field<?> createCustAccountFormField(String name) {
		Field<?> formField = null;
		String dispName = getCustAccountFieldDisplayName(name);
		boolean isNotNull = false;
		int length = accLengthMap.get(name);
		if (name.equals(CustAccountFields.ACCOUNTNO) || name.equals(CustAccountFields.REMARK)) {
			formField = MyFormFieldFactory.createStringField(name, dispName,length,isNotNull);
		}
		else if (name.equals(CustAccountFields.ACCOUNTTYPE)) {
			List<ListTypeModel<Integer>> listValueModels = new ArrayList<ListTypeModel<Integer>>();
			// 账户类型(1：基本账户，2：一般账户)
			listValueModels.add(new ListTypeModel<Integer>(CustConstant.ACCOUNT_TYPE_BASIC, CustAccountFieldsConstant.INSTANCE.account_basic()));
			listValueModels.add(new ListTypeModel<Integer>(CustConstant.ACCOUNT_TYPE_COMMON, CustAccountFieldsConstant.INSTANCE.account_common()));
			formField = MyFormFieldFactory.createListField(name, dispName, listValueModels, true,isNotNull);
		}
		else if (name.equals(CustAccountFields.BANK)) {
			formField = MyFormFieldFactory.createRefField(name, dispName, new BankRefDialog(),isNotNull);
		}
		else if (name.equals(CustAccountFields.ACCOUNTISDEFAULT)) {
			formField = MyFormFieldFactory.createCheckBooleanField(name, dispName);
		}
		else if (name.equals(CustAccountFields.ID)) {
			formField = MyFormFieldFactory.createIdField(name);
		}
		return formField;
	}

	public List<Field<?>> getFormFields(String[] fNames){
		List<Field<?>> fields = new ArrayList<Field<?>>(fNames.length);
		for (String name : fNames) {
			Field<?> formField = createFormField(name);
			if (formField == null) {
				continue;
			}
			fields.add(formField);
		}
		return fields;
	}

	/**
	 * 设置地区之间的父子关系。
	 * 
	 * @param pCust
	 * @param children
	 */
	public void setCustRelations(CustModel pCust, List<CustModel> children) {
		if(children==null||children.size()==0){
			return;
		}
		for (CustModel custModel : children) {
			custModel.setParentCust(pCust);
		}
	}

	public List<ColumnConfig> getCustGridColumnConfigs() {
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();
		configs.add(numberer);

		ColumnConfig column = new ColumnConfig();
		String name = CustModel.CODE;
		column.setId(name);
		column.setHeader(getCustFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = CustModel.NAME;
		column.setId(name);
		column.setHeader(getCustFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = CustModel.BUSINESS_ADDRESS;
		column.setId(name);
		column.setHeader(getCustFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = CustModel.CONTACT_ADDRESS;
		column.setId(name);
		column.setHeader(getCustFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		name = CustModel.ZIPCODE;
		column.setId(name);
		column.setHeader(getCustFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);

		return configs;
	}
	
	@SuppressWarnings("unchecked")
	public List<ColumnConfig> getCustAccountGridColumnConfigs() {
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
//		RowNumberer numberer = new RowNumberer();
//		configs.add(numberer);

		String name = CustAccountModel.BANK;
		final MyRefTextField<BankModel> refField=(MyRefTextField<BankModel>)createCustAccountFormField(name);
		CellEditor cellEditor = new MyRefCellEditor<BankModel>(refField);
		ColumnConfig column = new ColumnConfig();
		column.setEditor(cellEditor);
		column.setId(name);
		column.setHeader(getCustAccountFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);
		
		name = CustAccountModel.ACCOUNTNO;
		cellEditor = new CellEditor(createCustAccountFormField(name));
		column = new ColumnConfig();
		column.setEditor(cellEditor);
		column.setId(name);
		column.setHeader(getCustAccountFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);
		
		name = CustAccountModel.ACCOUNTTYPE;
		final MyListField<Integer> cbField = (MyListField<Integer>)createCustAccountFormField(name);
		cellEditor = new MyListCellEditor<Integer>(cbField);
		column = new ColumnConfig();
		column.setEditor(cellEditor);
		column.setId(name);
		column.setHeader(getCustAccountFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);
		
		name = CustAccountModel.ACCOUNTISDEFAULT;
		cellEditor = new CellEditor(createCustAccountFormField(name));
		CheckColumnConfig checkColumn = new CheckColumnConfig();
		checkColumn.setEditor(cellEditor);
		checkColumn.setId(name);
		checkColumn.setHeader(getCustAccountFieldDisplayName(name));
		checkColumn.setWidth(55);
		configs.add(checkColumn);

		return configs;
	}

	public String getCustFieldDisplayName(String methodName) {
		return CustFieldsConstant.INSTANCE.getString(methodName);
	}

	public String getCustAccountFieldDisplayName(String methodName) {
		return CustAccountFieldsConstant.INSTANCE.getString(methodName);
	}

	public CustModel createModelInstance(){
		return new CustModel();
	}

	@Override
	public String getCodeFieldName() {
		return CustModel.CODE;
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFields;
	}

}
